package org.grottarossa.ui.core.casemanagement.bpm.actionsbar;

import java.util.ArrayList;
import java.util.ResourceBundle;

import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.BasePanelView;
import org.grottarossa.ui.core.controls.searchuser.SearchUser;


import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window;

/***
 * Buttons bar di gestione lock/unlock/suspend/assign task
 * @author 
 *
 */
public class LockBar extends BasePanelView<LockBarPresenter> implements ClickListener
{
	private Button m_btnChangeLockStatus;
	private Button m_btnSuspend;
	private Button m_btnAssign;

	public LockBar(ApplicationSession appSession, ResourceBundle localizer)
	{
		super(appSession, localizer);
		initializeComponents();
	}

	public void initializeComponents()
	{
		HorizontalLayout bar = new HorizontalLayout();
		bar.setMargin(false, false, false, false);

		m_btnChangeLockStatus = new Button();
		m_btnChangeLockStatus.setIcon(new ThemeResource(m_localizer.getString("images.lock")));
		m_btnChangeLockStatus.addListener(this);

		m_btnSuspend = new Button();
		m_btnSuspend.addListener(this);
		m_btnSuspend.setIcon(new ThemeResource(m_localizer.getString("images.suspend")));

		m_btnAssign = new Button();
		m_btnAssign.addListener(this);
		m_btnAssign.setIcon(new ThemeResource(m_localizer.getString("images.assign")));

		bar.addComponent(m_btnSuspend);
		bar.addComponent(m_btnChangeLockStatus);
		bar.addComponent(m_btnAssign);
		addComponent(bar);
	}

	/***
	 * Imposta la visibilit� del pulsante di suspend
	 * @param visible
	 */
	public void setSuspendVisible(boolean visible)
	{
		m_btnSuspend.setVisible(visible);
	}

	/***
	 * Cambia l'icona del pulsante lock/unlock
	 * @param locked
	 */
	public void setChangeLockStatusButtonIcon(boolean locked)
	{
		String resource = (locked) ? "images.unlock" : "images.lock";
		m_btnChangeLockStatus.setIcon(new ThemeResource(m_localizer.getString(resource)));
	}

	public void buttonClick(ClickEvent event)
	{
		if(event.getButton() == m_btnSuspend)
			m_presenter.suspend();
		else if(event.getButton() == m_btnChangeLockStatus)
			m_presenter.changeLockStatus();
		else if(event.getButton() == m_btnAssign)
			m_presenter.assign();
	}

	/***
	 * Restituisce i pulsanti
	 * @return
	 */
	public ArrayList<Button> getButtons()
	{
		ArrayList<Button> buttons = new ArrayList<Button>();
		buttons.add(m_btnAssign);
		buttons.add(m_btnChangeLockStatus);
		buttons.add(m_btnSuspend);
		return buttons;
	}

	/***
	 * Restituisce la window sulla quale � visualizzato il pulsante di assign
	 * @return
	 */
	public Window getAssignButtonWindow()
	{
		return m_btnAssign.getWindow();
	}


}
